/** This Class manages the list entries of the item fields in the itemFieldSelectorPanel
 * @author zulianp@lu.unisi.ch
 */


package ch.unisi.inf.projects.java.uom.gui.itemeditor.fieldselector;

import java.awt.Component;
import javax.swing.BoxLayout;
import javax.swing.Box;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.unisi.inf.projects.java.uom.Parser;
import ch.unisi.inf.projects.java.uom.Constants;
import ch.unisi.inf.projects.java.uom.elements.Item;
import ch.unisi.inf.projects.java.uom.exceptions.UOMException;
import ch.unisi.inf.projects.java.uom.elements.ItemManager;
import ch.unisi.inf.projects.java.uom.elements.ItemClass;
import ch.unisi.inf.projects.java.uom.gui.commands.OpenListCommand;

public final class ListEntryGUI extends EntryGUI {
	
	private final JPanel panel;
	private static final int LABEL = 0;
	private static final int BUTTON = 1;
	private Component[] components;
	private String label;

	public ListEntryGUI(final String key, final String value, final JPanel containerPanel, final Item item) {
		super(key, value, item);
		this.panel = new JPanel();
		this.panel.setLayout(new BoxLayout(this.panel, BoxLayout.Y_AXIS));
		this.panel.setBackground(getDefaultColor());
		components = new Component[3];
		setLabel();
		containerPanel.add(components[LABEL]);
		containerPanel.add(this.panel);
		this.panel.add(Box.createVerticalGlue());
		setUp();
		this.panel.add(Box.createVerticalGlue());
	}
	private void setLabel()
	{
		label = null;
		try {
				label = Parser.instance().dbGetVarName(key);
		} catch (final UOMException e) {
			
			label = e.getMessage();
		}
		JLabel jLabel = new JLabel(label + " : ");
		//jLabel.setToolTipText(ItemHTMLConverter.convertItem(item,true));
		jLabel.setHorizontalAlignment(JLabel.RIGHT);
		jLabel.setBackground(getDefaultColor());
		components[LABEL] = jLabel;
	}
	
	
	private void setUp()
	{
		try {
			final String itemClassName = Parser.instance().dbGetListTypeRelationship(key);
			final ItemClass itemClass = ItemManager.instance().itemClassGet(itemClassName);
			if (itemClass == null && !itemClassName.equals(Constants.ITEM_CLASS_STRING.value())) {
				final JLabel errorLabel = new JLabel("Invalid reference category '" + itemClassName + "'");
				panel.add(errorLabel);
				return;
			}
		}
		catch (Exception e)
		{
			panel.add(new JLabel("Error while processing list"));
			return;
		}
		final JButton listLinkButton = new JButton(new OpenListCommand(item, key, value));
			
		components[BUTTON] = listLinkButton;
		listLinkButton.setBackground(getDefaultColor());
		panel.add(listLinkButton);
	}

	@Override
	public void select() {
		return;
	}

	@Override
	public boolean setFromInput() {
		return true;
	}
	
	public boolean isModified () {
		return false;
	}
	
	public void refresh ()
	{
		return;
	}

}
